//
//  GSInputView.m
//  GameSky
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSInputView.h"

#define kButtonWidth 40
#define kButtonHeight 30
#define kMargin 10

@implementation GSInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 0);
        self.layer.shadowOpacity = 0.5;
        
        // 取消按钮
        self.cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(kMargin, kMargin, kButtonWidth, kButtonHeight)];
        [self.cancelBtn setTitle:@"取消" forState:(UIControlStateNormal)];
        [self.cancelBtn dk_setTitleColorPicker:DKColorWithColors([UIColor blackColor], [UIColor grayColor]) forState:(UIControlStateNormal)];
        [self.cancelBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:self.cancelBtn];
        // 发送按钮
        self.postBtn = [[UIButton alloc] initWithFrame:CGRectMake(kWidth - kMargin - kButtonWidth, kMargin, kButtonWidth, kButtonHeight)];
        [self.postBtn setTitle:@"发送" forState:(UIControlStateNormal)];
        [self.postBtn setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
        [self addSubview:self.postBtn];
        // 游客评论
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kWidth / 2 - 65, kMargin, 60, kButtonHeight)];
        label.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor grayColor]);
        label.font = [UIFont systemFontOfSize:15];
        label.text = @"游客评论";
        [self addSubview:label];
        self.commentSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(kWidth / 2 + 5, kMargin, 60, kButtonHeight)];
        [self addSubview:self.commentSwitch];
        // 输入框
        self.textView = [[UITextView alloc] initWithFrame:CGRectMake(kMargin, self.cancelBtn.bottom + kMargin, kWidth - kMargin * 2, kHeight - kMargin * 2- self.cancelBtn.bottom)];
        self.textView.backgroundColor = [UIColor lightGrayColor];
        self.textView.font = [UIFont systemFontOfSize:15];
        [self addSubview:self.textView];
        // 监听TextView文本输入;
        [kNotificationCenter addObserver:self selector:@selector(postBtnEnable) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}
// 取消按钮点击方法;
- (void)cancelBtnClick:(UIButton *)button
{
    [_textView resignFirstResponder];
}
// 通知方法;
- (void)postBtnEnable
{
    _postBtn.enabled = _textView.text.length;
    if (_postBtn.enabled) {
        [_postBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    } else {
        [_postBtn setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateNormal)];
    }
}

- (void)dealloc
{
    [kNotificationCenter removeObserver:self];
}

@end
